<template>
    <u-index-plugins url="/plugins/flash_sale/index/index">
        <template v-slot:u-top-name>
            <view class="cross-center u-plugin-top">
                <view class="u-plugin-icon" :style="{'background-color': theme.background,'background-image': 'url('+appImg.icon_home_flash_sales+')'}"></view>
                <template v-if="newData.activity || newData.next_activity">
                    <view class="dir-left-nowrap time-box">
                        <view class="main-center cross-center time">{{time_str.hou}}</view>
                        <view class="main-center cross-center maohao">:</view>
                        <view class="main-center cross-center time">{{time_str.min}}</view>
                        <view class="main-center cross-center maohao">:</view>
                        <view class="main-center cross-center time">{{time_str.sec}}</view>
                    </view>
                    <view :class="time_str ? 'box-grow-0' : 'box-grow-1'" style="color: #353535;margin-left: 10rpx;font-size: 22rpx;">{{newData.str}}</view>
                </template>
            </view>
        </template>
        <template v-slot:u-body>
            <view class="scroll-list" :class="style === '2' ? 'main-between two-list':'dir-left-nowrap'">
                <app-goods
                  v-for="(goods,idx) in goodsList" :key="goods.id"
                  :index="idx"
                  :showTag="false"
                  :theme="theme"
                  :goods="goods"
                  :isUnderLinePrice="true"
                  :c_border_top="16"
                  :scrollWidth="304"
                  :isIndex="true"
                  :showBuyBtn="false"
                  :padding="48"
                  :listStyle="0">
                  </app-goods>
            </view>
        </template>
    </u-index-plugins>
</template>

<script>
import {mapGetters, mapState} from 'vuex';
import uIndexPlugins from '../u-index-plugins/u-index-plugins.vue';
import appGoods from '../../basic-component/app-goods/app-goods.vue';


export default {
    name: "u-flash-sale",
    props: {
        theme: Object,
        index: Number,
        page_id: Number,
        is_required: Boolean,
        appImg: {
            type: Object,
            default: function() {
                return {
                    plugins_out: ''
                }
            }
        },
        appSetting: {
            type: Object,
            default: function() {
                return {
                    is_show_stock: 1,
                    sell_out_pic: '',
                    is_use_stock: 1
                }
            }
        }
    },
    data() {
        return {
            newData: {},
            tempList: [],
            goodsList: [],
            time: 0,
            time_str:{
                hou: '00',
                min: '00',
                sec: '00'
            },
            timing: null,
            style: '1',
            goods_num: 20,
        };
    },
    components: {
        uIndexPlugins,
        appGoods
    },
    computed: {
        ...mapGetters('mallConfig', {
            getTheme: 'getTheme',
        }),
        copyList: function() {
            return this.newData.list;
        }
    },
    methods: {
        loadData() {
            let para = {
                type: this.page_id === 0 ? 'mall' : 'diy',
                key: 'flash_sale',
                page_id: this.page_id,
                index: this.index
            }
            if(this.goods_num) {
                para.goods_num = this.goods_num
            }
            this.$request({
                url: this.$api.index.extra,
                data: para
            }).then(e => {
                if (e.code === 0) {
                    this.newData = e.data;
                    if (this.newData.activity) {
                        this.newData.str = '结束';
                        this.set_time(this.newData.activity.end_at);
                    } else {
                        this.newData.str = '开始';
                        if (this.newData.next_activity && this.newData.next_activity.start_at) {
                            this.set_time(this.newData.next_activity.start_at);
                        }
                    }
                }
            })
        },
        // 复制而不是引用对象和数组
        cloneData(data) {
            return JSON.parse(JSON.stringify(data));
        },
        // 循环载入
        splitData() {
            if (!this.tempList.length) return;
            let item = this.tempList[0];
            this.goodsList.push(item);
            this.tempList.splice(0, 1);
            if (this.tempList.length) {
                this.timeOut = setTimeout(() => {
                    this.splitData();
                }, 200);
            }
        },
        set_time(time_at) {
            clearInterval(this.timing);
            let time_str = new Date(time_at.replace(/-/g, '/'));
            this.now_time(time_str);
            this.timing = setInterval(() => {
                this.now_time(time_str);
            }, 1000);
        },
        now_time(time_str) {
            let time = time_str.getTime() - new Date().getTime();
            if (time < 0) {
                clearInterval(this.timing);
            }

            let hou = parseInt(time/1000/60/60);
            let min = parseInt((time/1000/60)%60);
            let sec = parseInt((time/1000)%60);
            this.time_str.hou = hou < 10 ? '0' + hou : hou;
            this.time_str.min = min < 10 ? '0' + min : min;
            this.time_str.sec = sec < 10 ? '0' + sec : sec;
        },
    },
    mounted() {
        let storage = this.$storage.getStorageSync('INDEX_MALL');
        this.style = storage.home_pages[this.index].style;
        this.goods_num = storage.home_pages[this.index].goods_num;
        this.loadData();
    },
    watch: {
        copyList: {
            handler(newVal) {
                if (this.$validation.empty(newVal)) return;
                this.tempList = this.cloneData(newVal);
                this.splitData();
            }
        },
        pageHide: {
            handler(v) {
                if (v) {
                    clearInterval(this.timing);
                    return;
                } else {
                    if (this.newData.activity) {
                        this.newData.str = '结束';
                        this.set_time(this.newData.activity.end_at);
                    } else if (this.newData.next_activity) {
                        this.newData.str = '开始';
                        this.set_time(this.newData.next_activity.start_at);
                    }
                }

            },
            immediate: true
        },
    },
    destroyed() {
        clearTimeout(this.time);
    },
    beforeDestroy() {
        clearInterval(this.timing);
    },
}
</script>

<style scoped lang="scss">
    @import url('./index.scss');
    .time-box {
        .time {
            padding: 0 8rpx;
            height: #{40rpx};
            background-color: #666666;
            color: #ffffff;
            font-size: #{28rpx};
            border-radius: #{4rpx};
        }

        .maohao {
            width: #{14rpx};
            height: #{30rpx};
            color: #353535;
        }
    }
</style>